import platform
from urllib.parse import urlparse
import os

LINE_CHAR_COUNT = 20 * 2  # 每行字符数：30个中文字符(=60英文字符)
CHAR_SIZE = 30
TABLE_WIDTH = 4


# 从url获取文件名和文件后缀
def get_file_name_from_url(url):
    url_p = urlparse(url)
    file_name = os.path.basename(url_p.path)
    _, suffix = os.path.splitext(file_name)
    return file_name, suffix


class MyUtil(object):
    # 检测是否是linux操作系统
    @staticmethod
    def check_is_linux():
        ret = platform.system()
        if ret == 'Linux':
            return True
        return False

    # 在索引位置之前添加字符串
    @staticmethod
    def add_str_before(source, need_add, index):
        source = str(source)
        new_str = source[:index] + need_add + source[index:]
        return new_str

    # 超过自动换行
    @staticmethod
    def line_break(line):
        ret = ''
        width = 0
        for c in line:
            if len(c.encode('utf8')) == 3:  # 中文
                if LINE_CHAR_COUNT == width + 1:  # 剩余位置不够一个汉字
                    width = 2
                    ret += '\n' + c
                else:  # 中文宽度加2，注意换行边界
                    width += 2
                    ret += c
            else:
                if c == '\t':
                    space_c = TABLE_WIDTH - width % TABLE_WIDTH  # 已有长度对TABLE_WIDTH取余
                    ret += ' ' * space_c
                    width += space_c
                elif c == '\n':
                    width = 0
                    ret += c
                else:
                    width += 1
                    ret += c
            if width >= LINE_CHAR_COUNT:
                ret += '\n'
                width = 0
        if ret.endswith('\n'):
            return ret
        return ret + '\n'


if __name__ == '__main__':
    name, suffix = get_file_name_from_url("https://st-cn.meishij.net/rs/41/203/113291/n113291_154086913289864.jpg")
    print(name)
    print(suffix)
